﻿using Alos.Entity;
using Alos.IRepository;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alos.Repository
{
    public class TeachingMaterialRepository : BaseRepository<TeachingMaterial>, ITeachingMaterialRepository
    {
        /// <summary>
        /// 根据用户编号获取教材
        /// </summary>
        /// <param name="id"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public IEnumerable<dynamic> GetTeachingMaterialByUid(int id)
        {
            var sql = Sql.Builder.Append(" SELECT D.ID,F.SubjectName,D.[Version],D.SubVersion,CASE D.UpOrDown WHEN 1 THEN '上册' WHEN 2 THEN '下册' END AS UpOrDown FROM [dbo].[TeachingMaterial] D ");
            sql.LeftJoin(" [dbo].[DicSubject] F  ").On(" F.SubjectCode=D.SubjectCode ");

            sql.Where(" D.ID IN(SELECT B.MaterialID FROM [dbo].[TeachingMaterialBind] B WHERE B.GradeInfoId IN( SELECT A.GradeInfoId FROM [dbo].[BasicClassInfo] A WHERE A.ID IN(SELECT A.ClassID FROM [dbo].[BasicClassTERelation] A WHERE A.TeacherID=@0))) ", id);

            return DBContext.Query<dynamic>(sql);

        }
    }
}
